package org.xlx.xlx_oa.warehouse.controller;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.xlx.xlx_oa.common.BusinessException;
import org.xlx.xlx_oa.common.ResponseBean;
import org.xlx.xlx_oa.warehouse.api.WarehouseDistributorApi;
import org.xlx.xlx_oa.warehouse.domain.vo.DistributorVo;
import org.xlx.xlx_oa.warehouse.service.WarehouseDistributorService;

@RestController
@RequestMapping("/v1/warehouse-distributor")
public class WarehouseDistributorController implements WarehouseDistributorApi {
    @Autowired
    private WarehouseDistributorService distributorService;

    @PostMapping("/save")
    @Override
    public ResponseBean saveDistributor(@RequestBody DistributorVo distributorVo) {
        return ResponseBean.successFactory(distributorService.saveDistributor(distributorVo));
    }

    @DeleteMapping("/remove/{id}")
    @Override
    public ResponseBean removeDistributor(@PathVariable(value = "id") Long id) {
        try {
            return ResponseBean.successFactory(distributorService.removeDistributor(id));
        } catch (BusinessException b) {
            return ResponseBean.failureFactory(b.getCode(), b.getMessage());
        } catch (Exception e) {
            return ResponseBean.failureFactory(e.getMessage());
        }
    }

    @PostMapping("/list")
    @Override
    public ResponseBean getList(@RequestBody DistributorVo distributorVo) {
        return ResponseBean.successFactory(distributorService.getList(distributorVo));
    }

    @PostMapping("/count")
    @Override
    public ResponseBean getCount(@RequestBody DistributorVo distributorVo) {
        return ResponseBean.successFactory(distributorService.getCount(distributorVo));
    }

    @GetMapping("/detail/{id}")
    @Override
    public ResponseBean getDetail(@PathVariable(value = "id") Long id) {
        return ResponseBean.successFactory(distributorService.getDetail(id));
    }

    @PostMapping("/search")
    @Override
    public ResponseBean searchByCompanyName(@RequestParam(value = "companyName") String companyName) {
        return ResponseBean.successFactory(distributorService.searchByCompanyName(companyName));
    }
}

